<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="security"
	uri="http://www.springframework.org/security/tags"%>

<c:url value="/profile/flights/ajax" var="findflightsUrl" />
<script type="text/javascript">
	function bookflight(flightId) { 

		var token = $("meta[name='_csrf']").attr("content");
		var header = $("meta[name='_csrf_header']").attr("content");
		$.ajax({ 
			data : {},  
			url : "/profile/flights/" +    flightId + "/ajax",
			type : "POST",
			beforeSend : function(xhr) {
				xhr.setRequestHeader("Accept", "application/json");
				xhr.setRequestHeader("Content-Type", "application/json");
				xhr.setRequestHeader(header, token);
			},
			success : function(result) {
				if (obj) {
					var tr = $("#c" + flightId);
					tr.css("background-color", "#FF3700");

					tr.fadeOut(400, function() {
						tr.remove();
					});
				} else {
					$('#result').html("Error");
				}
			}
		});
	}
</script> 

<script type="text/javascript">
	function findFlights() {

		var json = {};
		var token = $("meta[name='_csrf']").attr("content");
		var header = $("meta[name='_csrf_header']").attr("content");
		$
				.ajax({
					type : "POST",
					url : "${findflightsUrl}",
					data : {
						endPlace : $("#end").val(),
						startPlace : $("#start").val(),
						vip : $("#vip").prop( "checked" ) , 
						baggage : $("#baggage").prop( "checked" )
					},
					beforeSend : function(xhr) {
						xhr.setRequestHeader("Accept", "application/json");
						xhr
								.setRequestHeader("Content-Type",
										"application/json");
						xhr.setRequestHeader(header, token);
					},
					success : function(result) {

						var row = "<tr><th>" + "Start Place"
								+ "</th><th>" + "End Place" + "</th><th>"
								+ "Departure Time" + "</td><td>" + "Price"
								+ "</th><th>" + "Action" + "</th></th>";

						$.each(result,function(index, item)
								{
									row += "<tr><td>" 
										+ item.startPlace
										+ "</td><td>"
										+ item.endPlace
										+ "</td><td>"
										+ item.departureTime
										+ "</td><td>"             
										+ item.price             
										+ "</td><td>"               
										+ '<a href="<c:url value="/profile/flights/${item.flightId}" />"> Edit </a>'
										+ "</td></tr>";

								});
						$("#flights").html(row); 
					},
					error : function() {
						alert('failure');
					}
				});
	}
</script>

<h3>Find Flights</h3>
<br />

<input type="text" name="start" id="start"
	placeholder="Enter Strat Place">
<input type="text" name="end" id="end" placeholder="Enter EndPlace">
VIP 
<input type="checkbox" name="vip" id="vip">
Baggage
<input type="checkbox" name="baggage" id="baggage">   
<input type="button" onclick="findFlights()" value="Find Flights">


<br />

<table id="flights" border="1"
	class="table table-striped table-bordered">


</table>